#!/bin/bash
# - lkvs_repo
# - lkvs_checkout
# - feature

echo "Cloning source code from $lkvs_repo..."

retry=0
max_retries=3
while ((retry<max_retries))
do
    git clone $lkvs_repo $TMP/lkvs && break
    retry=$((retry+1))
    echo "Git clone failed, retrying... ($retry/$max_retries)"
    sleep 5
done

if ((retry==max_retries)); then
    echo "Git clone failed after $max_retries attempts, exiting..."
    exit 1
fi

echo "Code downloaded, checking out to $lkvs_checkout..."
cd $TMP/lkvs && git checkout $lkvs_checkout || exit 1
echo "Ready."

if [[ -z $feature ]]; then
    echo "Feature to test could not be empty" && \
    exit 1
elif [[ ! -d $TMP/lkvs/$feature ]]; then
    echo "$feature does not exist in $lkvs_repo@$lkvs_checkout" && \
    exit 1
fi

echo "Running LKVS for testing $feature..."
./runtests -f $feature/tests
echo "Done."